package com.spr.utils;

import java.util.*;

public class ListUtils {
    /**
     * 比较一个list集合里是否有重复
     * */
    public static <T> boolean isRepeat(List<T> list) {
        Set<T> set = new HashSet<>(list);
        return set.size() != list.size();
    }

    /**
     * 比较两个集合是否有重复
     * */
    public static boolean isRepeat(List<Object> list1, List<Object> list2){
        Set<Object> set1 = new HashSet<>(list1);
        Set<Object> set2 = new HashSet<>(list2);
        Set<Object> setAll = new HashSet<>(set1);
        setAll.addAll(set2);
        int setSize = set1.size() + set2.size();
        return setAll.size() != setSize;
    }

    /**
     * 判断集合是否为空
     * @param list
     * @return
     */
    public static boolean isNull(List<Object> list){
        return list==null||list.size()==0;
    }

    /**
     * 判断集合是否不为空
     * @param list
     * @return
     */
    public static boolean isNotNull(List<Object> list){
        return list!=null&&list.size()>0;
    }


    /**
     * 将一个list分成长度的多个list
     * @param list
     * @param partLength
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> groupList(List<T> list, int partLength) {
        List<List<T>> listGroup = new ArrayList<>();
        int listSize = list.size();
        for (int i = 0; i < list.size(); i += partLength) {
            if (i + partLength > listSize) {
                partLength = listSize - i;
            }
            List<T> newList = list.subList(i, i + partLength);
            listGroup.add(newList);
        }
        return listGroup;
    }


    /**
     * 判断两个List内的元素是否相同
     *
     * @param list1
     * @param list2
     * @return
     */
    public static boolean isEquals(List<String> list1, List<String> list2) {
        if (list1==null && list2==null){
            return true;
        }
        if(list1 == null || list2 == null) {
            return false;
        }
        if (list1.size() != list2.size()) {
            return false;
        }
        for (String str : list1) {
            if (!list2.contains(str)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 将List中元素迁移到最后位置
     * @param t
     * @param list
     * @param <T>
     * @return
     */
    public  static  <T> boolean moveListEnd(T t,List<T> list){
        if (list==null || list.size()==0 || t==null || !list.contains(t)){
            return false;
        }
        if(list.remove(t)){
            return list.add(t);
        }
        return false;

    }

    public static void main(String[] args) {
//        List<Student> students =new ArrayList<>();
//        Student student = new Student();
//        student.setName("sss");
//        student.setIsMan(false);
//
//
//        Student student1 = new Student();
//        student1.setName("www");
//        student1.setIsMan(true);
//
//
//        Student student2 = new Student();
//        student2.setName("mmm");
//        student2.setIsMan(false);
//
//
//        Student student3 = new Student();
//        student3.setName("aaa");
//        student3.setIsMan(false);
//
//
//        Student student4 = new Student();
//        student4.setName("kkk");
//        student4.setIsMan(true);
//
//        students.add(student);
//        students.add(student1);
//        students.add(student2);
//        students.add(student3);
//        students.add(student4);
//        System.out.println("students = " + students);
//        List<Student> collect = students.stream().sorted(new Comparator<Student>() {
//            @Override
//            public int compare(Student student, Student t1) {
//                if (student.getIsMan()) {
//                    return 1;
//                } else {
//                    return -1;
//                }
//            }
//        }).collect(Collectors.toList());
//        System.out.println("collect = " + collect);

        List<Integer> awardsList = new ArrayList<>();

        awardsList.add(1);
        awardsList.add(2);
        awardsList.add(3);
        awardsList.add(1);
        awardsList.remove(1);
//        if (CollectionUtils.isEmpty(awardsList)){
            System.out.println("awardsList = " + awardsList);
//        }
//        awardsList.add("ss");
//        awardsList.add("a");
//        awardsList.add("rre");
//        awardsList.add("iis");
//        awardsList.add("oss");
//        awardsList.add("cw");
//        String join = String.join(",", awardsList);
//        System.out.println("join = " + join);
//        System.out.println("awardsList = " + awardsList);

//        join = ss,a,rre,iis,oss,cw
//        awardsList = [ss, a, rre, iis, oss, cw]

    }

}
